package cn.imvc.app.models;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.imvc.util.BiService;
import cn.imvc.util.HttpHelper;

@Service
public class BiLogisticsMateModelImpl implements BiLogisticsMateModel{
	@Autowired
	BiService biService;

	public List<Object[]> logisticsMate(List<Object[]> objsList) throws DocumentException {
		List<Object[]> returnObjsList = new ArrayList<>();
		if(!objsList.isEmpty()){
			Iterator<Object[]> iterator = objsList.iterator();
			StringBuffer orderXmlSb = new StringBuffer();
			while (iterator.hasNext()) {
				Object[] objects = (Object[]) iterator.next();
				orderXmlSb.append("<order>\n");
				orderXmlSb.append("<ecpCode>").append(biService.getEcpCodeCus())
						.append("</ecpCode>\n");
				orderXmlSb.append("<orderNo>"+objects[1].toString()+"</orderNo>\n");
				orderXmlSb.append("</order>\n");
			}
			// 生成请求报文
			String xml = generateXml(orderXmlSb.toString());
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("xml", xml);
			// 调用webservice服务
			String PostResult = HttpHelper.sendHttpPOSTRequest(biService.getLogisticsMateUrl(), map);
			// 查看响应回执
//			System.out.println("======报文回执:======\n" + PostResult);
			Document doc = DocumentHelper.parseText(PostResult);
			Element rootElem = doc.getRootElement();
			Iterator<Element> pubIter = rootElem.elementIterator("pub");
			String txtSuccess = "";
			String txtErrorMsg =  "";
			while (pubIter.hasNext()) {
				Element element = (Element) pubIter.next();
				txtSuccess = element.elementTextTrim("success");
				txtErrorMsg = element.elementTextTrim("errorMsg");
			}
			//如果发送成功了
			if("0".equals(txtSuccess)){
				Iterator<Element> ordersIter = rootElem.elementIterator("orders");
				String txtOrderNo = "";
				String txtLogisticsNo = "";
				while (ordersIter.hasNext()) {
					Element element = (Element) ordersIter.next();
					Iterator<Element> orderIter = element.elementIterator("order");
					while (orderIter.hasNext()) {
						Element element2 = (Element) orderIter.next();
						txtOrderNo = element2.elementTextTrim("orderNo");
						txtLogisticsNo = element2.elementTextTrim("logisticsNo");
					}
					Object[] objs = {txtOrderNo,txtLogisticsNo};
					returnObjsList.add(objs);
				}
			}else{
				//如果失败了
				System.out.println("-----------------失败了："+txtErrorMsg);
			}
		}
		return returnObjsList;
	}

	/**
	 * 生成请求报文数据
	 * 
	 * @param encryptDataInfo
	 * @return
	 */
	private String generateXml(String orders) {
		StringBuffer xmlSb = new StringBuffer();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateNowStr = dateFormat.format(new Date());
		xmlSb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		xmlSb.append("<root>\n");
		xmlSb.append("	<pub>\n");
		xmlSb.append("			<companyCode>").append(biService.getCompanyCode())
				.append("</companyCode>\n");
		xmlSb.append("			<companyKey>").append(biService.getCompanyKey())
				.append("</companyKey>\n");
		xmlSb.append("			<requestTime>"+dateNowStr+"</requestTime>\n");
		xmlSb.append("	</pub>\n");
		xmlSb.append("	<orders>").append(orders)
				.append("</orders>\n");
		xmlSb.append("</root>\n");
		return xmlSb.toString();
	}
}
